package com.yueyang.sojourn.server.controller.wx;

import com.github.pagehelper.PageInfo;
import com.yueyang.sojourn.server.common.consts.Constants;
import com.yueyang.sojourn.server.controller.BaseController;
import com.yueyang.sojourn.server.model.enums.ShareDataTypeEnum;
import com.yueyang.sojourn.server.model.vo.SojournMerchVo;
import com.yueyang.sojourn.server.service.share.ShareDataService;
import com.yueyang.sojourn.server.service.sojourn.collect.SojournMerchCollectService;
import com.yueyang.sojourn.server.service.sojourn.merch.ReadMerchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Stephen.Shi
 * @version v1
 * @summary 商品收藏controller
 * @since 2020/2/20
 */
@RestController
@RequestMapping("/api/wx/sojourn/merch/collect")
@Api(tags = "(小程序)商品收藏接口")
public class ApiMerchCollectController extends BaseController {
    @Autowired
    private SojournMerchCollectService collectService;
    @Autowired
    private ReadMerchService readMerchService;
    @Autowired
    private ShareDataService shareDataService;

    @ApiOperation(value="收藏商品", notes="收藏商品")
    @PostMapping
    public void collect(@RequestParam("merchId") Long merchId,
                        @RequestHeader(value = "shareId", required = false) Long shareId,
                        @RequestHeader(Constants.LOGIN_TOKEN) String token) {
        collectService.collect(merchId, getLoginUserId());
        if (shareId != null) {
            shareDataService.action(shareId, ShareDataTypeEnum.收藏, getLoginUserId(), "收藏了商品", null);
        }
    }

    @DeleteMapping
    @ApiOperation(value="取消收藏", notes="取消收藏")
    public void cancel(@RequestParam("merchId") Long merchId,
                        @RequestHeader(Constants.LOGIN_TOKEN) String token) {
        collectService.cancelCollect(merchId, getLoginUserId());
    }

    @ApiOperation(value="收藏列表", notes="收藏列表")
    @GetMapping("/list")
    public PageInfo<SojournMerchVo> list(@RequestParam("pageNum") int pageNum,
                                         @RequestParam("pageSize") int pageSize,
                                         @RequestHeader(Constants.LOGIN_TOKEN) String token) {
        return this.readMerchService.findCollectPage(getLoginUserId(), pageNum, pageSize);
    }


}
